﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;
using System.Collections;
using Catering.View_Operaction;
using Catering_DBOpertion;

namespace Catering.yl.Daily
{
    public partial class ContratorMember : System.Web.UI.Page
    {        
        int contractorID = -1;        
        protected void Page_Load(object sender, EventArgs e)
        {          
            if (!Page.IsPostBack)
            {                
                Session["CurrentDate"] = DateTime.Now.ToString("yyyy/MM/dd");
                if(Session["ConMemberID"] != null)
                {
                    contractorID = Convert.ToInt32(Session["ConMemberID"].ToString());
                    DataSet ds = SelConNameByCID(Session["ConMemberID"].ToString());
                    DataTable dt = ds.Tables[0];
                    foreach (DataRow dr in dt.Rows)
                    {
                        Session["ConMember"] = dr[0].ToString();
                    }
                }                
                LoadForm();
            }
        }
        public void LoadForm()
        {
            try
            {
                DataTable dt_null = null;
                this.Rep_Plan.DataSource = dt_null;
                this.Rep_Plan.DataBind();
                if (Session["ConMemberID"] != null)
                {
                    DataTable dt_Plan = new Catering_Rules.ContratorMember().GetList(" CID = " + Session["ConMemberID"].ToString()).Tables[0];
                    dt_Plan.Columns.Add("tempId");
                    dt_Plan.Columns.Add("ClassName");
                    dt_Plan.Columns.Add("OldClassName");

                    for (int i = 0; i < dt_Plan.Rows.Count; i++)
                    {
                        dt_Plan.Rows[i]["tempId"] = i + 1;
                        if(dt_Plan.Rows[i]["ClassID"].ToString() != "")
                        {
                            DataSet ds_className = SelClassInfoAndClassID(dt_Plan.Rows[i]["ClassID"].ToString());
                            DataTable dt_className = ds_className.Tables[0];
                            dt_Plan.Rows[i]["ClassName"] = dt_className.Rows[0]["CName"];
                        }
                        else
                            dt_Plan.Rows[i]["ClassName"] = "";
                        if (dt_Plan.Rows[i]["OldClassID"].ToString() != "")
                        {
                            DataSet ds_oldclassName = SelClassInfoAndClassID(dt_Plan.Rows[i]["OldClassID"].ToString());
                            DataTable dt_oldclassName = ds_oldclassName.Tables[0];
                            dt_Plan.Rows[i]["OldClassName"] = dt_oldclassName.Rows[0]["CName"];
                        }
                        else
                            dt_Plan.Rows[i]["OldClassName"] = "";
                        for (int j = 0; j < dt_Plan.Columns.Count; j++ )
                        {
                            if (dt_Plan.Rows[i][j].ToString() == "" || dt_Plan.Rows[i][j].ToString() == null)
                            {
                                dt_Plan.Rows[i][j] = "&nbsp";
                            }
                        }
                    }
                this.Rep_Plan.DataSource = dt_Plan;
                this.Rep_Plan.DataBind();
                }                
            }
            catch
            {
            }
        }
        public DataSet SelClassInfoAndClassID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 CName,AreaID");
            strSql.Append(" FROM ClassInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where id=" + int.Parse(strWhere));
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        //添加
        [WebMethod]
        public static string Add(string Name, string OClass, string NClass, string CardNum, string CardName, string CRegion, string CPhone, string CDesc, string gongzhong, string hdn_useDate, string hdn_currentClassID, string hdn_currentGroupID, string hdn_contractorID)
        {
            Catering_Model.ContratorMember _cm = new Catering_Model.ContratorMember();
            _cm.CID = int.Parse(hdn_contractorID);
            _cm.CName = Name;
            _cm.GroupID = "";
            _cm.ClassID = "";
            _cm.CardNo = CardNum;
            _cm.JobCard = CardName;
            _cm.Effective = CRegion;
            _cm.Tel = CPhone;
            _cm.ConDesc = CDesc;
            _cm.gongzhong = gongzhong;
            _cm.OldClassID = "";
            _cm.OldGroupID = "";
            _cm.ChangeClassDate = Convert.ToDateTime(hdn_useDate);
            new Catering_Rules.ContratorMember().Add(_cm);
            return "OK";
        }
        [WebMethod]
        public static ArrayList Get(string _id)
        {
            ArrayList al = new ArrayList();
            Catering_Model.ContratorMember md = new Catering_Rules.ContratorMember().GetModel(Convert.ToInt32(_id));
            al.Add(md.Id);
            al.Add(md.CName);
            al.Add(md.OldClassID);
            al.Add(md.ClassID);
            al.Add(md.CardNo);
            al.Add(md.JobCard);
            al.Add(md.Effective);
            al.Add(md.Tel);
            al.Add(md.ConDesc);
            al.Add(md.gongzhong);
            return al;
        }
        [WebMethod]
        public static string Edit(string _id, string Name, string OClass, string NClass, string CardNum, string CardName, string CRegion, string CPhone, string CDesc, string gongzhong, string hdn_useDate)
        {
            int id = 0;
            int areaid = 0;
            Int32.TryParse(_id, out id);
            if (id <= 0) return "NO";

            Catering_Model.ContratorMember _CE = new Catering_Model.ContratorMember();
            _CE.Id = int.Parse(_id);
            _CE.CName = Name;
            _CE.ClassID = NClass;
            _CE.CardNo = CardNum;
            _CE.JobCard = CardName;
            _CE.Effective = CRegion;
            _CE.Tel = CPhone;
            _CE.ConDesc = CDesc;
            _CE.gongzhong = gongzhong;
            _CE.OldClassID = OClass;
            _CE.ChangeClassDate = Convert.ToDateTime(hdn_useDate);
            new Catering_Rules.ContratorMember().Update(_CE);

            return "OK";
        }
        protected void Rep_Plan_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int delID = Convert.ToInt32(e.CommandArgument);
                if (new Catering_Rules.ContratorMember().Delete(delID))
                {
                    this.LoadForm();
                }
            }
        }
        public DataSet SelConNameByCID(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 CName");
            strSql.Append(" FROM Contractor ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where id=" + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }     
    }
}